﻿using System;
using System.Linq.Expressions;
using System.Reflection;

namespace Fee.IO
{
    public static class LambdaExtensions
    {
        public static void SetPropertyValue<T>(this T target, Expression<Func<T, string>> memberLamda, string value)
        {
            var memberSelectorExpression = memberLamda.Body as MemberExpression;
            if (memberSelectorExpression != null)
            {
                var property = memberSelectorExpression.Member as PropertyInfo;
                if (property != null) property.SetValue(target, value, null);
            }
        }

        public static void SetPropertyValue<T>(this T target, Expression<Func<T, decimal>> memberLamda, decimal value)
        {
            var memberSelectorExpression = memberLamda.Body as MemberExpression;
            if (memberSelectorExpression != null)
            {
                var property = memberSelectorExpression.Member as PropertyInfo;
                if (property != null) property.SetValue(target, value, null);
            }
        }

        public static void SetPropertyValue<T>(this T target, Expression<Func<T, DateTimeOffset>> memberLamda,
            DateTimeOffset value)
        {
            var memberSelectorExpression = memberLamda.Body as MemberExpression;
            if (memberSelectorExpression != null)
            {
                var property = memberSelectorExpression.Member as PropertyInfo;
                if (property != null) property.SetValue(target, value, null);
            }
        }

        public static void SetPropertyValue<T>(this T target, Expression<Func<T, DateTimeOffset?>> memberLamda,
            DateTimeOffset? value)
        {
            var memberSelectorExpression = memberLamda.Body as MemberExpression;
            if (memberSelectorExpression != null)
            {
                var property = memberSelectorExpression.Member as PropertyInfo;
                if (property != null) property.SetValue(target, value, null);
            }
        }

        public static void SetPropertyValue<T>(this T target, Expression<Func<T, int>> memberLamda,
            object value)
        {
            var memberSelectorExpression = memberLamda.Body as MemberExpression;
            if (memberSelectorExpression != null)
            {
                var property = memberSelectorExpression.Member as PropertyInfo;
                if (property != null) property.SetValue(target, value, null);
            }
        }

        public static void SetPropertyValue<T>(this T target, Expression<Func<T, int?>> memberLamda,
            int? value)
        {
            var memberSelectorExpression = memberLamda.Body as MemberExpression;
            if (memberSelectorExpression != null)
            {
                var property = memberSelectorExpression.Member as PropertyInfo;
                if (property != null) property.SetValue(target, value, null);
            }
        }

        public static void SetPropertyValue<T>(this T target, Expression<Func<T, bool>> memberLamda,
            bool value)
        {
            var memberSelectorExpression = memberLamda.Body as MemberExpression;
            if (memberSelectorExpression != null)
            {
                var property = memberSelectorExpression.Member as PropertyInfo;
                if (property != null) property.SetValue(target, value, null);
            }
        }
    }
}